<?php

namespace Apipad\Controller;
header("content-type:application/json;charset=utf-8");

class ChengpOrderController extends CommonController
{


    //打印功能
    public function order_print()
    {
        $detail = I('post.');
        $uid = UID;
        $order_id = $detail['order_id'];
        if (empty($order_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单ID不能为空';
            exit(json_encode($data));
        }
        $detail = I('post.');
        $order_id = $detail['order_id'];
        if (empty($order_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单ID不能为空';
            exit(json_encode($data));
        }
        $o_model = M("user_order");
        $od_model = D('UserOrderDetail');
        $g_model = M('user_goods');
        $gd_model = D("UserGoodsAttr");
        $o_info = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($o_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单信息不存在';
            exit(json_encode($data));
        }
        $amount = 0;
        $total_amount = 0;
        $map = [];
        $map['order_id'] = $order_id;
        $map['uid'] = $uid;
        $map['isdel'] = 0;
        $od_list = $od_model->where($map)->field('goods_name,goods_sn,goods_attr,color,guige_name,price,discount,number')->select();
        if ($od_list) {
            foreach ($od_list as $k => $val) {
                $od_list[$k]['total'] = round($val['number'] * $val['price'] * $val['discount'], 2);
                $od_list[$k]['discount_price'] = round($val['price'] * $val['discount'], 2);
            }
        }
        $user_info = M('user')->where(['uid' => $uid])->field('names,zhifubao_pay,weixin_pay')->find();
        $info['sale_time'] = date('Y/m/d H:i', $o_info['add_time']);
        $info['store_names'] = M('user_config')->where(['uid' => $uid])->getField('names');
        $info['sale_type'] = '普通销售';
        $info['sale_number'] = $o_info['goods_number'];
        $info['sale_total'] = $o_info['amount'];
        $info['payment_total'] = $o_info['amount'] - $o_info['payment_amount'];
        $info['moling'] = $o_info['payment_amount'];
        $info['buy_total'] = $o_info['amount'] - $o_info['payment_amount'];
        $info['qiankuan'] = $o_info['amount'] - $o_info['payment_amount'];
        $info['store_cc'] = '默认库房';
        $info['customer'] = $o_info['customer_name'];
        $info['mobile'] = $o_info['mobile'];
        $info['sale_user'] = $user_info['names'];
        $info['fahuo'] = '自提';
        $info['weixin_pay'] = $user_info['weixin_pay'];
        $info['zhifubao_pay'] = $user_info['zhifubao_pay'];
        $info['remark'] = $o_info['remark'];
        $info['ordersn'] = $o_info['ordersn'];
        $info['dis_amount'] = $o_info['dis_amount'];
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['info'] = $info;
        $data['data']['list'] = $od_list;
        exit(json_encode($data));
    }

    //列表
    public function index()
    {
        $detail = I('');
        $dsafe = [];
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 20;
        $start_time = $detail['start_time'];
        $end_time = $detail['end_time'];
        $model = M('user_order');
        $map = [];
        if (!empty($start_time) || !empty($end_time)) {
            if (empty($start_time)) {
                $start_time = date('Y-m-d H:i:s', time());
            }
            if (empty($end_time)) {
                $end_time = date('Y-m-d H:i:s', time());
            }
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            $map['add_time'] = array('between', [$start_time, $end_time]);
        }

        if (I('keyword')) {
            $map['ordersn|customer_name|mobile'] = array('like', '%' . I('keyword') . '%');
        }
        $map['isdel'] = 0;
        $map['uid'] = UID;
        $map['stype'] = 1;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page, $limit)->select();
        foreach ($list as $k => $v) {
            $list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
        }
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //查看详情
    public function detail()
    {
        $detail = I('post.');
        $order_id = $detail['order_id'];
        if (empty($order_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单ID不能为空';
            exit(json_encode($data));
        }
        $keyword = I('keyword');
        $o_model = M("user_order");
        $od_model = D('UserOrderDetail');
        $g_model = M('user_goods');
        $gd_model = D("UserGoodsAttr");
        $o_info = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($o_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单信息不存在';
            exit(json_encode($data));
        }
        $amount = 0;
        $total_amount = 0;
        $total_number = 0;
        $map = [];
        if ($keyword) {
            $map['goods_name|goods_sn|guige_name|color'] = array('like', '%' . $keyword . '%');
        }
        $map['order_id'] = $order_id;
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $od_list = $od_model->where($map)->select();
        foreach ($od_list as $k => $v) {
            $g_info = $g_model->where(['id' => $v['goods_id'], 'uid' => UID, 'isdel' => 0])->find();
            $gd_info = $gd_model->where(['id' => $v['goods_attr_id'], 'goods_id' => $v['goods_id'], 'uid' => UID, 'isdel' => 0])->find();
            $od_list[$k]['stock'] = $gd_info['stock'] ?: 0;
            $od_list[$k]['img'] = $g_info['goods_img'] ?: '';
            $od_amount = 0;
            $od_dis_amount = 0;
            if ($v['discount'] > 0) {
                $dis_price = bcmul($v['price'], $v['discount'], 2);
            } else {
                $dis_price = $v['price'];
            }
            $od_amount = bcmul($v['price'], $v['number'], 2);
            $amount = bcadd($amount, $od_amount, 2);
            $od_dis_amount = bcmul($dis_price, $v['number'], 2);
            $od_list[$k]['xd_amount'] = $od_dis_amount ?: 0;
            $total_amount = bcadd($total_amount, $od_dis_amount, 2);
            $total_number = bcadd($total_number, $v['number'], 2);
        }
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['total_amount'] = floatval($total_amount);
        $data['data']['amount'] = floatval($amount);
        $data['data']['total_number'] = floatval($total_number);
        $data['data']['list'] = $od_list;
        exit(json_encode($data));
    }

    //订单删除
    public function order_del()
    {
        $detail = I('post.');
        $order_id = $detail['order_id'];
        if (empty($order_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单ID不能为空';
            exit(json_encode($data));
        }
        $o_model = M("user_order");
        $od_model = D('UserOrderDetail');
        $o_info = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($o_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单信息不存在';
            exit(json_encode($data));
        }
        $o_model->startTrans();
        $res = $o_model->where(['id' => $order_id, 'uid' => UID])->save(['isdel' => 1, 'del_time' => 0]);
        if ($res) {
            $result = $od_model->where(['order_id' => $order_id, 'uid' => UID])->save(['isdel' => 1, 'del_time' => 0]);
            if ($result) {
                $o_model->commit();
                $data['code'] = 200;
                $data['msg'] = '删除成功';
                exit(json_encode($data));
            } else {
                $data['code'] = 0;
                $data['msg'] = '删除失败';
                exit(json_encode($data));
            }
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }

    //规格详情删除
    public function order_detail_del()
    {
        $detail = I('');
        $order_detail_id = $detail['order_detail_id'];
        if (empty($order_detail_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单详情ID不能为空';
            exit(json_encode($data));
        }
        $od_model = M('user_order_detail');
        $od_info = $od_model->where(['id' => $order_detail_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($od_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单详情数据不存在';
            exit(json_encode($data));
        }
        $od_model->startTrans();
        $res = $od_model->where(['id' => $order_detail_id, 'uid' => UID])->save(['isdel' => 1, 'del_time' => time()]);
        if ($res) {
            $this->order_amount_change($od_info['order_id']);
            $od_model->commit();
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }

    }

    //编辑
    public function edit()
    {
        $detail = I('post.');
        $order_id = $detail['order_id'];
        if (empty($order_id)) {
            $data['code'] = 0;
            $data['msg'] = '订单ID不能为空';
            exit(json_encode($data));
        }
        $o_model = M("user_order");
        $od_model = D('UserOrderDetail');
        $o_info = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($o_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单信息不存在';
            exit(json_encode($data));
        }
        $od_model->startTrans();
        $order_detail_id = $detail['order_detail_id'];
        $order_num = $detail['order_num'];
        $discount = $detail['discount'];
        foreach ($order_detail_id as $k => $v) {
            $detail_id = $v;
            if (empty($detail_id)) {
                $data['code'] = 0;
                $data['msg'] = '订单详情ID不能为空';
                exit(json_encode($data));
            }
            if (empty($order_num[$k])) {
                $data['code'] = 0;
                $data['msg'] = '请输入下单数量';
                exit(json_encode($data));
            }
            $res = $od_model->where(['id' => $detail_id, 'uid' => UID, 'isdel' => 0])->save(['number' => floatval($order_num[$k]), 'discount' => floatval($discount[$k])]);
        }
        $this->order_amount_change($order_id);
        $od_model->commit();
        $data['code'] = 200;
        $data['msg'] = '编辑成功';
        exit(json_encode($data));

    }

    function order_amount_change($order_id)
    {
        $o_model = M("user_order");
        $od_model = D('UserOrderDetail');
        $o_info = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($o_info)) {
            $data['code'] = 0;
            $data['msg'] = '订单信息不存在';
            exit(json_encode($data));
        }
        $amount = 0;
        $total_amount = 0;
        $order_number = 0;
        $od_list = $od_model->where(['order_id' => $order_id, 'uid' => UID, 'isdel' => 0])->select();
        foreach ($od_list as $k => $v) {
            $od_amount = 0;
            $od_dis_amount = 0;
            if ($v['discount'] > 0) {
                $dis_price = bcmul($v['price'], $v['discount'], 2);
            } else {
                $dis_price = $v['price'];
            }
            $od_amount = bcmul($v['price'], $v['number'], 2);
            $amount = bcadd($amount, $od_amount, 2);
            $od_dis_amount = bcmul($dis_price, $v['number'], 2);
            $total_amount = bcadd($total_amount, $od_dis_amount, 2);
            $order_number = bcadd($order_number, $v['number']);
        }
        $res = $o_model->where(['id' => $order_id, 'uid' => UID, 'isdel' => 0])->save(['amount' => $amount, 'dis_amount' => $total_amount, 'goods_number' => $order_number]);
    }

}
